﻿package nl.stroep.classbuilder.app
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
	import nl.stroep.classbuilder.cb.CbClass;
	import nl.stroep.classbuilder.cb.CbMethod;
	import nl.stroep.classbuilder.cb.CbParameter;
	import nl.stroep.classbuilder.cb.CbVariable;
	import nl.stroep.classbuilder.codeformatter.CodeFormatter;
	import nl.stroep.classbuilder.definitions.AccessModifiers;
	import nl.stroep.classbuilder.definitions.DataTypes;
	import nl.stroep.classbuilder.definitions.FunctionModifier;
	import nl.stroep.classbuilder.definitions.VariableModifiers;
	import nl.stroep.classbuilder.project.Project;
	import nl.stroep.classbuilder.project.ProjectManager;
	
	/**
	 * ...
	 * @author Mark Knol
	 */
	public final class TestProjectSave extends Sprite 
	{
		private var myProject:Project;
		private var projectManager:ProjectManager;
		
		public function TestProjectSave():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			
			projectManager = ProjectManager.getInstance();
			
			myProject = new Project("myProject");
			myProject.settings.author = "Mark Knol"
			
			projectManager.add(myProject);
			
			createValueObjectTest();
			createEventTest();
			
			projectManager.save();
		}
		
		private function onProjectLoaded(e:Event):void 
		{
			var textfield:TextField = new TextField()
			textfield.defaultTextFormat = new TextFormat("Consolas", 11, 0, false);
			textfield.condenseWhite = true;
			textfield.text = CodeFormatter.format( projectManager.current.classes[0].toString() );
			textfield.width = stage.stageWidth * 0.5;
			textfield.height = textfield.textHeight + 10;
			textfield.x = stage.stageWidth * 0.5;
			textfield.wordWrap = true;
			this.addChild(textfield);
		}
		
		private function createEventTest():void
		{
			var startTime:Number = getTimer();
			
			var var_ADDED:CbVariable = new CbVariable("ADDED", DataTypes.STRING, "added", AccessModifiers.PUBLIC, VariableModifiers.STATIC, true);
			var var_LOADED:CbVariable = new CbVariable("LOADED", DataTypes.STRING, "loaded", AccessModifiers.PUBLIC, VariableModifiers.STATIC, true);
			
			var params:Vector.<CbParameter> = new Vector.<CbParameter>;
			params.push( new CbParameter("id", "int", null, true, true ) )
			params.push( new CbParameter("label", "String", null, true, false ) );
			
			params.push( new CbParameter("type", "String", null, false, false ) );
			params.push( new CbParameter("bubbles", "Boolean", "false", false, false ) );
			params.push( new CbParameter("cancelable", "Boolean", "false", false, false ) );
			
			var cloneMethod:CbMethod = new CbMethod("clone", "void", null, AccessModifiers.PUBLIC, null, "return new MyEvent(id,label,type,bubles,cancelable);" );
			
			var cbEventClass:CbClass = new CbClass("MyEvent");
			cbEventClass.type = DataTypes.EVENT;
			cbEventClass.implementsClass = "IEventDispatcher";
			cbEventClass.packAge = "nl.myproject.events";
			cbEventClass.author = myProject.settings.author;
			cbEventClass.constructorParams = params;
			cbEventClass.addMethod(cloneMethod)
			cbEventClass.addVariable(var_ADDED)
			cbEventClass.addVariable(var_LOADED)
			
			myProject.addClass( cbEventClass );
			
			trace("Calculated event in "  , getTimer() - startTime, "ms");
			
		}
		
		private function createValueObjectTest():void
		{
			var startTime:Number = getTimer();
			
			var myVar1:CbVariable = new CbVariable("myFirstLabel", DataTypes.STRING, "hello world", AccessModifiers.PUBLIC, null, false, true, true);
			var myVar2:CbVariable = new CbVariable("mySecondLabel", "String", null, AccessModifiers.PUBLIC, null, false, true, false);
			var myVar3:CbVariable = new CbVariable("myID", "int", null, AccessModifiers.PUBLIC, VariableModifiers.STATIC, false);
			
			// create global class functions
			var myFunc1:CbMethod = new CbMethod("doSomething", "void");
			
			var myFunc2:CbMethod = new CbMethod("doSomethingWithParams", "Array");
			myFunc2.addParameter( new CbParameter("id","int" ) );
			myFunc2.addParameter( new CbParameter("name", "String", null, false ) );
			myFunc2.addParameter( new CbParameter("data", "Object", "null", true ) );
			
			var myFunc3:CbMethod = new CbMethod("addChild", "Sprite", null, AccessModifiers.PUBLIC, FunctionModifier.OVERRIDE);
			
			// create constructor params			
			
			var params:Vector.<CbParameter> = new Vector.<CbParameter>;
			params.push( new CbParameter("id","int", null, true, true ) );
			params.push( new CbParameter("label", "String", null, true, false ) );
			params.push( new CbParameter("sprite", "Sprite", "null", true, false ) );
			params.push( new CbParameter("clip", "MovieClip", "null", true, true ) );
			params.push( new CbParameter("textfield", "TextField", "null", false, false ) );
			
			// create final class
			var cbClass:CbClass = new CbClass("MyCustomClass");
			cbClass.type = "MovieClip";
			cbClass.packAge = "nl.myproject.vo";
			cbClass.author = myProject.settings.author;
			cbClass.constructorParams = params;
			cbClass.addMethod(myFunc1)
			cbClass.addMethod(myFunc2)
			cbClass.addMethod(myFunc3)
			cbClass.addVariable(myVar1)
			cbClass.addVariable(myVar2)
			cbClass.addVariable(myVar3)
			
			trace("Calculated vo in "  , getTimer() - startTime, "ms");
			
			var textfield:TextField = new TextField()
			textfield.defaultTextFormat = new TextFormat("Consolas", 11, 0, false);
			textfield.condenseWhite = true;
			textfield.text = CodeFormatter.format( cbClass.toString() );
			textfield.wordWrap = true;
			textfield.width = stage.stageWidth * 0.5;
			textfield.height = textfield.textHeight + 10;
			this.addChild(textfield);
			
			myProject.addClass( cbClass );
		}
		
	}
	
}